package com.kelaite.procurement.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Date Util
 * @author Allen
 */
public class ConvertTime {

	/**
	 * yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		return format.format(date);
	}

	/**
	 * yyyy-MM-dd HH:mm
	 * 
	 * @return
	 */
	public static String getTime2() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = new Date();
		return format.format(date);
	}

	/**
	 * yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String getTime3(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);
	}

	/**
	 * yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getShortTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		return format.format(date);
	}

	/**
	 * yyyyMMdd
	 * 
	 * @return
	 */
	public static String getDateTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		return format.format(date);
	}

	/**
	 * yyyyMMdd
	 * @return
	 */
	public static String getDateTimeString(String dateString) {
		Date date = StringToDate(dateString);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		return format.format(date);
	}

	public static String getDateTime2() {
		SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
		Date date = new Date();
		return format.format(date);
	}

	/**
	 * @return yyyyMMddhhmmss
	 */
	public static String getTimeFile() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date();
		return format.format(date);
	}
	
	/**
	 * @return yyyyMMddhhmmss
	 */
	public static String getTimeFile3() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
		Date date = new Date();
		return format.format(date);
	}
	/**
	 * @return yyyyMMddhhmm
	 */
	public static String getTimeFile2() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmm");
		Date date = new Date();
		return format.format(date);
	}

	/**
	 * 
	 * @param target
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static Date StringToDate(String target) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = sdf.parse(target);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * Convert String to Date type	 * @param target
	 * @return yyyy-MM-dd HH:mm
	 */
	public static Date StringToDate3(String target) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			date = sdf.parse(target);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * Convert Date to String type.	 * 
	 * @param date
	 * @return yyyy-MM-dd HH:mm
	 */
	public static String getStringFromDate(Date date) {
		if(date == null){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}

	/**
	 * @param date
	 * @return yy-MM-dd HH:mm
	 */
	public static String getStringFromDate2(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm");
		return sdf.format(date);
	}

	/**
	 * 
	 * @param target
	 * @return yyyy-MM-dd
	 */
	public static Date StringToDate2(String target) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(target);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * @param target
	 * @return MM-dd HH:mm
	 */
	public static String dateToString(String target) {
		Date date = ConvertTime.StringToDate(target);
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		try {
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * @param target
	 * @return MM-dd HH:mm
	 */
	public static String dateToString2(String target) {
		Date date = ConvertTime.StringToDate(target);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * util.Date--->sql.Date
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Date getSQLDate(Date date) {
		java.sql.Date a = new java.sql.Date(date.getTime());
		return a;
	}

	/**
	 * format: 2012-4-16
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToStringType1(Date date) {
		return DateFormat.getDateInstance(DateFormat.DEFAULT).format(date);
	}
	
	public static String formatSQLDataToString(java.sql.Date date){
		long time = date.getTime();
		return getTime3(new Date(time));
	}

	/**
	 * output format: 2006年4月16日 星期六	 * @param date
	 * @return
	 */
	public static String dateToStringType2(Date date) {
		return DateFormat.getDateInstance(DateFormat.AM_PM_FIELD).format(date);
	}

	/**
	 * @param time
	 * @return 0-10
	 */
	public static String cutTime(String time) {
		return time.substring(0, 10);
	}

	/**
	 * 
	 * @param date
	 * @param period
	 *            间隔分钟
	 * @return
	 */
	public static boolean ifDisconnect(Date date, int period) {
		long judgeDate = date.getTime();
		long nowDate = new Date().getTime();
		if (nowDate - judgeDate > period * 60 * 1000) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Date-->Timestamp
	 * @param date
	 * @return Timestamp
	 */
	public static java.sql.Timestamp getTimeStampDate(Date date) {
		try {
			java.sql.Timestamp a = new java.sql.Timestamp(date.getTime());
			return a;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param target
	 * @return
	 */
	public static int getDayPeriod(String target) {
		int days = 0;
		try {
			long after = StringToDate2(target).getTime();
			long now = new Date().getTime();
			days = (int) Math.floor((after - now) / (60 * 60 * 24 * 1000)); // floor
			// 小于等于
			if (days < 0) {
				return 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return days;
	}

	/**
	 * 返回在附加了指定天数的日期	 * @param targetTime
	 *            附加日期
	 * @param day
	 *            附加天数
	 * @return yyyy年MM月dd日	 */
	public static String plusDaysString(String targetTime, int day) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date targetDate = sdf.parse(targetTime);
			Date newDate = new Date(targetDate.getTime() + day
					* (long) (3600 * 24 * 1000));
			sdf = new SimpleDateFormat("yyyy年MM月dd日");
			return sdf.format(newDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 返回在附加了指定天数的日期
	 * @param targetTime
	 *            附加日期
	 * @param day
	 *            附加天数
	 * @return yyyy年MM月dd日
	 */
	public static Date plusDaysDate(Date date, int day) {
		try {
			Date newDate = new Date(date.getTime() + day
					* (long) (3600 * 24 * 1000));
			return newDate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param targetTime
	 * @return yyyy年MM月dd日
	 */
	public static String chineseDateString1(String targetTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date targetDate = sdf.parse(targetTime);
			sdf = new SimpleDateFormat("yyyy年MM月dd日");
			return sdf.format(targetDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * @param date
	 * @return yyyy年MM月dd日	 */
	public static String chineseDateString2(Date date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 格式化日期
	 * @param date
	 * @return yyyy-MM-dd
	 */
	public static String englishDateString2(Date date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 
	 */
	public final static String dayNames[] = { "星期日", "星期一", "星期二", "星期三",
			"星期四", "星期五", "星期六" };
	
	public final static String dayNames_En[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
		"Thursday", "Friday", "Saturday" };
	/**
	 * 得到日期对应的星期数
	 * 
	 * @param dateString
	 * @return 星期*
	 */
	public static String returnWhichWeek1(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		try {
			date = sdf.parse(dateString);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			return dayNames[dayOfWeek - 1];
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 得到日期对应的星期数
	 * 
	 * @param date
	 * @return 星期*
	 */
	public static String returnWhichWeek2(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(getTime3(date));
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			return dayNames[dayOfWeek - 1];
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 根据一个时间字符串得到, 2009-06-19 10:12:55 -> 2009年6月19日 星期几
	 * @param 
	 * @return String
	 */
	public static String returnLastLoginTime(String target) {
		String yearInfo = "";
		String weekInfo = "";
		try {
			Date date = StringToDate(target);//转化为date
			SimpleDateFormat sdf = null;
			sdf = new SimpleDateFormat("yyyy年MM月dd日");
			yearInfo = sdf.format(date);
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			
			weekInfo = dayNames[dayOfWeek - 1];
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return yearInfo + " " + weekInfo;
	}
	
	
	/**
	 * 返回一周的时间段	 * @param date
	 * @return 如: 2009-01-05~2009-01-09
	 */
	public static String weekPeriod(Date date) {
		String nowDateString = ConvertTime.chineseDateString2(date);
		long beforeTime = 3600 * 24 * 4 * 1000;
		Date beforeDate = new Date(date.getTime() - beforeTime);
		String beforeDateString = ConvertTime.chineseDateString2(beforeDate);
		return beforeDateString + "~" + nowDateString;
	}
	
	public static boolean isDate1BeforeData2(Date date1, Date date2){
		return date1.getTime() < date2.getTime();
	}
	
	public static boolean isDate1BeforeOrEqualData2(Date date1, Date date2){
		return date1.getTime() <= date2.getTime();
	}
	
}
